# @author: Yxinmiracle
# @project: Python_w
# @file: getData.py
# @time: 2020/5/16 21:03


import requests
import re
import json
from shixiseng.config import WOFF_FILE_NAME,XML_NAME,HOST,headers,font_dict,RE_RULE
from shixiseng.getOther import get_font_file,get_glyph_dict,get_xml_dict
from urllib.parse import quote

rule = re.compile(RE_RULE)

get_font_file() # 得到字体文件

url = "https://www.shixiseng.com/app/interns/search/v2?build_time=1589639520283&page=1&keyword=Python&type=intern&area=&months=&days=&degree=&official=&enterprise=&salary=-0&publishTime=&sortType=&city=%E6%B7%B1%E5%9C%B3&internExtend="
html = requests.get(url, headers=headers)

glyph_dict = get_glyph_dict() # 得到xml中name与他相对应的字体坐标一一对应的关系
xml_dict = get_xml_dict() # 得到xml中code与name的对应关系

"""
本代码的字典对应关系：xml_dict --> glyph_dict ---> font_dict
假设：  1. 在text中找到&#xe004，我们就在xml_dict中找到与之对应的uni6708，
        2. 再去glyph_dict中找到uni6708与之对应的(-45, -386)
        3. 最后去font_dict里面找(-45, -386)与之对应的："月"
        4. 替换掉原先字符中的&#xe004

当然这字典的对应关系可以变得简单一点，但是中途的逻辑会变成复杂，所以没必要，那样别人就会读不懂
"""


def get_replaced_str(replace_data):  # 将replace_data替换成想没有乱码的

    replaceStringList = re.findall(rule,replace_data)

    for replaceString in replaceStringList:
        if replaceString in xml_dict:
            glyphName = xml_dict[replaceString]
            location = glyph_dict[glyphName]
            result = font_dict[location]
            replace_data = re.sub(replaceString, result, replace_data)

    return replace_data

json_data = json.loads(html.text) # 解析网页返回的数据

for datas in json_data['msg']['data']:
    maxsalary = get_replaced_str(datas['maxsal'])
    minsalary = get_replaced_str(datas['minsal'])
    name = get_replaced_str(datas['name'])
    need_people_num = get_replaced_str(datas['scale'])
    industry = get_replaced_str(datas['industry'])
    refresh_day = get_replaced_str(datas['refresh'])
    uuid = get_replaced_str(datas['uuid'])
    inner_url = f"https://www.shixiseng.com/intern/{uuid}?pcm=pc_SearchList&mxa=asdd.0eqlx1._.$1"
    print(minsalary,maxsalary,name,need_people_num,industry,refresh_day,inner_url)

# if __name__ == '__main__':
#     NAME_LIST = [('java')]